<?php

namespace app\controller\Api\V1\Client;

use app\model\GameAuthorization;
use Exception;
use support\Request;
use support\Response;

class VoiceChatController
{

    /**
     * 获取初始配置
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function Init(Request $request): Response
    {

        requestValidator($request, [
            'game' => 'bail|required|exists:app\\model\\GameAuthorization,ulid,state,1,deleted_at,NULL',
            'area' => 'bail|required|exists:app\\model\\DistrictService,ulid,state,1,deleted_at,NULL',
            'role_id' => 'bail|required|string',
            'role_name' => 'bail|required|string',
        ], [
            'game.required' => '游戏不能为空',
            'game.exists' => '游戏不存在',
            'area.required' => '区服不能为空',
            'area.exists' => '区服不存在',
            'role_id.required' => '请选择角色',
            'role_id.string' => '角色ID不正确',
            'role_name.required' => '请选择角色',
            'role_name.string' => '角色名称不正确',
        ]);

        $gameInfo = GameAuthorization::query()->where('ulid', $request->input('game'))->where('state', 1)->first();
        if(!$gameInfo || empty($gameInfo->Merchant)) {
            throw new Exception('游戏不存在', 500);
        }

        return jsonRe([
            'config' => [
                'state' => $gameInfo->Merchant->voice_chat_switch === 1 ?? false,
            ]
        ]);

    }
    
}